<template>
  <h2 class="text-base font-semibold leading-7 text-gray-900">
    {{ $t('pageRebootBmc.lastReboot') }}
  </h2>
  <p v-if="lastBmcRebootTime">
    {{ globalStore.formatDateTime(lastBmcRebootTime) }}
  </p>

  <p class="mt-5 w-96 font-semibold text-lg">
    {{ $t('pageRebootBmc.rebootInformation') }}
  </p>
  <el-button class="mt-5" type="primary" @click="confirmRebootBmc">{{
    $t('pageRebootBmc.rebootBmc')
  }}</el-button>
</template>

<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import { useControlStore } from '@/store/Settings/ControlStore';
import { useGlobalStore } from '@/store/global';

const controlStore = useControlStore();
const globalStore = useGlobalStore();

await controlStore.getLastBmcRebootTime();

const i18n = reactive(useI18n());

const lastBmcRebootTime = computed(() => {
  if ('Invalid Date' == controlStore.lastBmcRebootTime) {
    return false;
  }
  return controlStore.lastBmcRebootTime;
});

const confirmRebootBmc = () => {
  ElMessageBox.confirm(
    i18n.t('pageRebootBmc.modal.confirmMessage'),
    i18n.t('pageRebootBmc.modal.confirmTitle'),
    {
      confirmButtonText: i18n.t('global.action.confirm'),
      cancelButtonText: i18n.t('global.action.cancel'),
      type: 'warning',
    },
  )
    .then(() => {
      rebootBmc();
    })
    .catch(() => {});
};
const rebootBmc = () => {
  controlStore
    .rebootBmc()
    .then((message: string) => {
      ElNotification({
        type: 'success',
        message,
      });
    })
    .catch((message: string) => {
      ElNotification({
        type: 'error',
        message,
      });
    });
};
</script>

<style lang="scss"></style>
